#ifndef __SM__
#define __SM__
#include "Type_Define.h"
#include "IO_LINK_Global_Param.h"

//SM中状态变量定义
typedef enum{
   SM_PortInactive_0,
   SM_CheckCompatiblity_1,
   SM_waitonDLPreoperate_2,
   SM_checkSerNum_3,
   SM_wait_4,
   SM_SMOperate_5,
   SM_InspectionFault_6,
   SM_waitonDLOperate_7,
   SM_DIDO_8,
   SM_JoinPseudoState_9,
   SM_ReadComParameter_20,
   SM_sub_Read_Page_0x02,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x03,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x04,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x05,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x06,//自定义子状态，用于等待page读结果
   //主动进入，Event_Page_Cnf退出
   SM_CheckCompV10_21,
   SM_sub_Read_Page_0x07,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x08,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x09,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x0A,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x0B,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x0C,//自定义子状态，用于等待page读结果
   SM_sub_Read_Page_0x0D,//自定义子状态，用于等待page读结果
   SM_CheckVxy_22,
   SM_CheckComp_23,
   SM_RestartDevice_24,
   SM_sub_Write_Page_0x04, //自定义子状态，用于等待page读结果
   SM_sub_Write_Page_0x00,//自定义子状态，用于等待page读结果
   SM_sub_Write_Page_0x09,//自定义子状态，用于等待page读结果
    SM_sub_Write_Page_0x0A,//自定义子状态，用于等待page读结果
     SM_sub_Write_Page_0x0B,//自定义子状态，用于等待page读结果
   SM_JoinPseduoState_25,
   SM_ReadSerNum_30,   
   SM_CheckSerNum_31

}SM_STATE_TYPE;


//SM中变量类型定义
typedef enum{
	COMLOST,
 STARTUP,
 INACATIVE,
 PREOPERATE,
 OPERATE
 
}SM_DL_STATE_TYPE;

typedef enum{
  NOT_DETECTED=0,
  COM_Speed1=1,
  COM_Speed2=2,
  COM_Speed3=4
}COM_Speed_TYPE;

typedef enum{
  SM_INACTIVE,
  SM_DI,
  SM_DO,
  SM_CFGCOM,
  SM_AUTOCOM
}SM_TargetMode_TYPE;

typedef enum{
  SM_NO_CHECK,
  SM_TYPE_COMP,
  SM_IDENTICAL
}SM_InspectionLevel_TYPE;

typedef struct{
   uint8_t Port;
   uint8_t ConfiguredCycleTime;
   SM_TargetMode_TYPE TargetMode;
   uint8_t CRID;
   SM_InspectionLevel_TYPE InspectionLevel;
   uint16_t CVID;
   uint32_t CDID;
   uint16_t CFID;
   uint8_t CSN[16];

}SM_SetPortConfig_Arg_TYPE;

 typedef struct{
   uint8_t Port; 
	 SM_TargetMode_TYPE TargetMode;
   COM_Speed_TYPE  RealBaudrate;
   uint8_t RealCycleTime;
   uint8_t RRID; 
   uint16_t RVID;
   uint32_t RDID;
   uint16_t RFID;
   uint8_t CSN[16];
 }SM_GetPortConfig_Arg_TYPE;

 typedef enum{
    SM_Mode_INACTIVE,
    SM_Mode_DI,
    SM_Mode_DO,
    SM_Mode_COMREADY,
    SM_Mode_SM_OPERATE,
    SM_Mode_COMLOST,
    SM_Mode_REVISION_FAULT,
    SM_Mode_COMP_FAULT,
    SM_Mode_SERNUM_FAULT,
    SM_Mode_CYCTIME_FAULT
 }SM_PortMode_Mode_TYPE;


extern SM_DL_STATE_TYPE SM_DL_STATE[PORT_NUM]; //SM中记录的DL状态，由DL_Mode.ind申报
extern COM_Speed_TYPE SM_COM_Speed[PORT_NUM];//SM中记录DL的速度，由DL_Mode.ind申报
extern SM_STATE_TYPE SM_sub_STATE[PORT_NUM]; //

void Event_DL_Mode_STARTUP(uint8_t Port);

void Event_DL_Mode_PREOPERATE(uint8_t Port);

void Event_DL_Mode_OPERATE(uint8_t Port);

void Event_DL_Mode_COMLOST(uint8_t Port);

void Event_DL_Page_Cnf(uint8_t Port); //Read和WriteCnf用一个事件

void Event_SM_SRead_Nagtive(uint8_t Port);

extern uint8_t Serial_Number[PORT_NUM][16];
extern uint16_t VendorID[PORT_NUM];
extern uint32_t DeviceID[PORT_NUM];
extern uint8_t RevisionID[PORT_NUM];
extern uint16_t FunctionID[PORT_NUM];
uint16_t  Service_SM_SetPortConfig_req(SM_SetPortConfig_Arg_TYPE ParameterList );
void Service_SM_PortMode(uint8_t Port,SM_PortMode_Mode_TYPE Mode);
uint16_t Service_SM_Operate_req(uint8_t Port);
#endif



